package com.halixun.d2app.ui;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.google.gson.JsonObject;
import com.halixun.d2app.MainActivity;
import com.halixun.d2app.R;
import com.halixun.d2app.base.BaseActivity;
import com.halixun.d2app.base.BaseResult;
import com.halixun.d2app.net.CreateRequestEntity;
import com.halixun.d2app.net.errer.ResultVerifier;
import com.halixun.d2app.utils.RequestDataUtils;
import com.halixun.d2app.utils.SystemUtil;
import com.halixun.d2app.utils.ToastUtils;
import com.halixun.d2app.utils.VersionUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BoundActivity extends BaseActivity {

    private EditText guidEdit;
    private TextView tvDevice;
    private String deviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bound);
        hideActionBar();
        initView();
        setListeners();
        initData();
    }

    @SuppressLint("NewApi")
    private void initData() {
        deviceId = SystemUtil.getIMEI(this);
        tvDevice.setText(deviceId);
    }

    private void setListeners() {
        findViewById(R.id.confirmBound).setOnClickListener(this);
    }

    private void initView() {
        guidEdit = findViewById(R.id.guidEdit);
        tvDevice = findViewById(R.id.tv_device);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.confirmBound:
                toBound();
                break;
        }
    }

    @SuppressLint("NewApi")
    private void toBound() {
        if (TextUtils.isEmpty(deviceId)){
            ToastUtils.showBottomToast("设备信息获取失败，请重试");
            return;
        }
        String guid = guidEdit.getText().toString().trim();
        if (TextUtils.isEmpty(guid)){
            ToastUtils.showBottomToast("请输入GUID");
            return;
        }
        showProgressDialog("绑定中...");
        JsonObject object = new JsonObject();
        object.addProperty("guid",guid);
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), object.toString());
        CreateRequestEntity.getWebService().bindDevice(body, RequestDataUtils.getRequestDataParam(this)).enqueue(new Callback<BaseResult<String>>() {
            @Override
            public void onResponse(Call<BaseResult<String>> call, Response<BaseResult<String>> response) {
                dismissProgressDialog();
                if (ResultVerifier.isSucceed(response)){
                    ToastUtils.showBottomToast("绑定成功");
                    toSkipLogin(false);
                } else {
                    ResultVerifier.serceErrers(response);
                }
            }

            @Override
            public void onFailure(Call<BaseResult<String>> call, Throwable t) {
                dismissProgressDialog();
                ResultVerifier.systerErrers(t);
            }
        });
    }

    private void toSkipLogin(boolean loginState) {
        startActivity(new Intent(this, loginState ? MainActivity.class : LoginActivity.class));
        finish();
    }
}
